﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using PlaylistEditorForCentrafuse.GUI;

namespace PlaylistEditorForCentrafuse.Playlist
{
    public class PlaylistEntry : IComparable<PlaylistEntry>
    {
        public PlaylistFile Playlist { get; set; }
        
        private string _filename;
        private string _title;
        private bool _isMissing;
        private bool _isDuplicate = false;
        private PlaylistAudioFile _audiofile;

        private string _interpret;
        private string _album;
        private string _genre;
        private string _songnr;
        private string _year;
        private string _date1;
        private string _date2;
        private string _empty1;
        private string _empty2;

        private ListViewGroup _missingGroup = null;
        private ListViewGroup _duplicateGroup = null;
        private ListViewGroup _okGroup = null;
        private ListViewItem _listItem = null;

        // CF3 Style
        public PlaylistEntry(PlaylistFile i_playlist,
                             PlaylistAudioFile i_audiofile,                 
                             string i_filename, 
                             string i_title, 
                             string i_interpret,
                             string i_album,
                             string i_genre,
                             string i_songnr,
                             string i_year,
                             string i_date1,
                             string i_date2,
                             string i_empty1,
                             string i_empty2)
        {
            Playlist = i_playlist;
            _audiofile = i_audiofile;
            _isMissing = _audiofile == null ? true : false;

            _filename = i_filename;
            _title = i_title;
            _interpret = i_interpret;
            _album = i_album;
            _genre = i_genre;
            _songnr = i_songnr;
            _year = i_year;
            _date1 = i_date1;
            _date2 = i_date2;
            _empty1 = i_empty1;
            _empty2 = i_empty2;
        }

        public int CompareTo(PlaylistEntry other)
        {
            int artistCompare = _interpret.CompareTo(other._interpret);

            if (artistCompare == 0)
            {
                // Bei gleichem Interpreten nach Titel sortieren
                return Title.CompareTo(other.Title);
            }
            else
            {
                // Sonst nach Interpret sortieren
                return artistCompare;
            }
        }

        public void SetListItem(ListViewItem i_item, ListViewGroup i_missingGroup, 
                                                     ListViewGroup i_duplicateGroup, 
                                                     ListViewGroup i_okGroup)
        {
            _listItem = i_item;
            _missingGroup = i_missingGroup;
            _duplicateGroup = i_duplicateGroup;
            _okGroup = i_okGroup;

            UpdateListView();
        }

        public void RefreshDetails()
        {
            if (_audiofile != null)
            {
                _audiofile.LoadTags(true);
                if (_audiofile.ID3v2 != null)
                {
                    _title = _audiofile.ID3v2.Title;
                    _interpret = _audiofile.ID3v2.Artist;
                    _album = _audiofile.ID3v2.Album;
                    _genre = _audiofile.ID3v2.Genre;
                    _songnr = _audiofile.ID3v2.Track;
                    _year = _audiofile.ID3v2.Year;
                }
                else
                {
                    _title = _audiofile.GetTitleFromFilename();
                    _interpret = _audiofile.GetArtistFromFilename();
                }

                _date1 = _date2 = DateTime.Now.ToString();
                _empty1 = _empty2 = "0";

                Playlist.Changed = true;
                UpdateListView();
            }
        }

        private void UpdateListView()
        {
            if (_listItem != null)
            {
                _listItem.Text = Path.GetFileName(_filename);
                _listItem.SubItems[(int)frmEditor.ColumnsPlaylistContents.Title].Text = _title;
                _listItem.SubItems[(int)frmEditor.ColumnsPlaylistContents.Artist].Text = _interpret;
                _listItem.SubItems[(int)frmEditor.ColumnsPlaylistContents.Album].Text = _album;
                _listItem.SubItems[(int)frmEditor.ColumnsPlaylistContents.Genre].Text = _genre;
                _listItem.SubItems[(int)frmEditor.ColumnsPlaylistContents.Track].Text = _songnr;
                _listItem.SubItems[(int)frmEditor.ColumnsPlaylistContents.Year].Text = _year;

                // Gruppe
                // Missing
                if (_isMissing)
                {
                    _listItem.Group = _missingGroup;
                    _listItem.ForeColor = Color.Red;
                }
                // Duplikat
                else if (_isDuplicate)
                {
                    _listItem.Group = _duplicateGroup;
                    _listItem.ForeColor = Color.Orange;
                }
                // Normal
                else
                {
                    _listItem.Group = _okGroup;
                    _listItem.ForeColor = Color.Green;
                }

                // Image
                //bool attachedImage = false;
                if (_audiofile != null)
                {
                    if (_audiofile.ID3v2 != null && _audiofile.ID3v2.Picture != null)
                    {
                        _listItem.ImageKey = _audiofile.FileName;
                    }
                }// if (_audiofile != null)
                else
                {
                    _listItem.ImageIndex = 0;
                }
            }// if (_listItem != null)
        }

        /////////////////////////////////////////
        // Getters / Setters
        /////////////////////////////////////////
        #region Getters / Setters
        public string Filename
        {
            get
            {
                return _filename;
            }
            set
            {
                _filename = value;
                //Playlist.Changed = true;
                //UpdateListView();
            }
        }

        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public string Interpret
        {
            get
            {
                return _interpret;
            }
            set
            {
                _interpret = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public string Album
        {
            get
            {
                return _album;
            }
            set
            {
                _album = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public string Genre
        {
            get
            {
                return _genre;
            }
            set
            {
                _genre = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public string SongNr
        {
            get
            {
                return _songnr;
            }
            set
            {
                _songnr = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public string Year
        {
            get
            {
                return _year;
            }
            set
            {
                _year = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public string Date1
        {
            get
            {
                return _date1;
            }
            set
            {
                _date1 = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public string Date2
        {
            get
            {
                return _date2;
            }
            set
            {
                _date2 = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public string Empty1
        {
            get
            {
                return _empty1;
            }
            set
            {
                _empty1 = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public string Empty2
        {
            get
            {
                return _empty2;
            }
            set
            {
                _empty2 = value;
                Playlist.Changed = true;
                UpdateListView();
            }
        }

        public bool IsMissing
        {
            get
            {
                return _isMissing;
            }
        }

        public bool IsDuplicate
        {
            get
            {
                return _isDuplicate;
            }
            set
            {
                _isDuplicate = value;
                UpdateListView();
            }
        }

        public PlaylistAudioFile Audiofile
        {
            get
            {
                return _audiofile;
            }
            set
            {
                _audiofile = value;
                _isMissing = _audiofile == null ? true : false;
                UpdateListView();
            }
        }

        public ListViewItem ListItem
        {
            get
            {
                return _listItem;
            }
        }
        #endregion
    }
}